package org.dizena.modules.master.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.dizena.common.db.ByPageQuery;
import org.dizena.common.pojo.PageReq;
import org.dizena.common.pojo.PageResp;
import org.dizena.common.pojo.master.PcUserVO;
import org.dizena.common.pojo.master.UserVO;
import org.dizena.modules.master.bean.User;
import org.dizena.modules.master.dao.UserDao;
import org.dizena.modules.master.service.UserService;
import org.dizena.modules.master.zload.ConstantMaster;
import org.dizena.utils.IdGen;
import org.dizena.utils.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao dao;

    @Override
    public PageResp<User> list(PageReq req) {
        ByPageQuery<User> byPageQuery = new ByPageQuery<>(req);
        List<User> list = dao.find(byPageQuery);
        long count = dao.count(byPageQuery);
        return new PageResp<>(req.getPage(), req.getSize(), count, list);
    }

    @Override
    public String add(UserVO vo) {
        User user = new User();
        user.setPlatform("mgr");
        BeanUtils.copyProperties(vo, user);
        return add(user);
    }


    @Override
    public String add(User user) {
        user.setId(IdGen.get().nextIdS());
        User old = login(user.getAccount());
        if (old != null) {
            return "account exist";
        }
        user.setLocked(ConstantMaster.NORMAL);
        user.setCreateTime(System.currentTimeMillis());
        user.setUpdateTime(System.currentTimeMillis());
        dao.insert(user);
        return "ok";
    }

    @Override
    public String edit(User user) {
        user.setSalt(null);
        user.setPasswd(null);
        user.setCreateTime(null);
        user.setUpdateTime(System.currentTimeMillis());
        dao.update(user);
        return "ok";
    }

    @Override
    public String del(String ids) {
        String[] ss = ids.split(",");
        for (String id : ss) {
            User tmp = dao.findOne(id);
//           if (StringUtils.isNotEmpty(tmp.getPic())) {
//                if (tmp.getPic().contains(assets))
//                {
//                    aliYunAssetsHy3dsOss.deleteFile(assets, tmp.getPic());
//                }
//            }
            dao.delete(id);
        }
        return "ok";
    }

    @Override
    public void unBindUser(User user) {
        dao.delete(user.getId());
        dao.insert(user);
    }

    @Override
    public User get(String id) {
        return dao.findOne(id);
    }

    @Override
    public String lockUser(String id) {
        String b = "locked";
        User user = dao.findOne(id);
        User upUser = new User();
        upUser.setId(id);
        if (ConstantMaster.LOCKED.equals(user.getLocked())) {
            upUser.setLocked(ConstantMaster.NORMAL);
            b = "normal";
        } else {
            upUser.setLocked(ConstantMaster.LOCKED);
        }
        upUser.setUpdateTime(System.currentTimeMillis());
        dao.update(upUser);
        return b;
    }

    @Override
    public String updatePassword(String id, String oldPassword, String newPassword, String newPassword2) {
        User user = get(id);
        if (user != null) {
            String passwd = org.springframework.util.DigestUtils.md5DigestAsHex((user.getSalt() + oldPassword).getBytes(StandardCharsets.UTF_8));
            if (passwd.equals(user.getPasswd())) {
                if (newPassword.equals(newPassword2)) {
                    User upUser = new User();
                    upUser.setId(id);
                    String salt = StrUtil.makeNumCode();
                    String pwd = org.springframework.util.DigestUtils.md5DigestAsHex((salt + newPassword).getBytes(StandardCharsets.UTF_8));
                    upUser.setSalt(salt);
                    upUser.setPasswd(pwd);
                    upUser.setUpdateTime(System.currentTimeMillis());
                    dao.update(upUser);
                    return "ok";
                }
                return "two new password not same";
            }
            return "old password is error";
        }
        return "user is not exist";

    }

    @Override
    public long countAll() {
        return dao.count();
    }

    @Override
    public User info() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Object principal = subject.getPrincipal();
            if (principal != null) {
                String account = principal.toString();
                return dao.findOne("account", account);
            }
        }
        return null;
    }

    @Override
    public void init() {
        Long ts = System.currentTimeMillis();
        User admin = new User("1", "admin", "管理员", "master", "*", ConstantMaster.NORMAL, ts);
        admin.setPlatform("mgr");
        //默认密码 12345678
        String salt = StrUtil.makeNumCode();
        String p1 = "admin";
        String passwd = org.springframework.util.DigestUtils.md5DigestAsHex((salt + p1).getBytes(StandardCharsets.UTF_8));
        admin.setPasswd(passwd);
        admin.setSalt(salt);
        dao.insert(admin);
    }

    @Override
    public String logout() {
        return null;
    }

    @Override
    public User login(String account) {
        return dao.findOne("account", account);
    }

    @Override
    public User wxOpen(String openId) {
        return dao.findOne("openIdWx", openId);
    }

    @Override
    public User fsOpen(String openId) {
        return dao.findOne("openIdFs", openId);
    }

    @Override
    public PcUserVO modify(PcUserVO m) {
        User user = info();
        assert user != null;
        User tmp = new User();
        tmp.setId(user.getId());
        BeanUtils.copyProperties(m, tmp);
        dao.update(tmp);
        BeanUtils.copyProperties(info(), m);
        return m;
    }

    @Override
    public void cancel(String account) {
        User user = login(account);
        User tmp = new User();
        tmp.setId(user.getId());
        tmp.setLocked(ConstantMaster.CLOSE);
        tmp.setUpdateTime(System.currentTimeMillis());
        dao.update(tmp);
    }

    @Override
    public List<User> getCancelUser() {
        return dao.findCancelUser();
    }

    @Override
    public List<User> getUserNoMobile() {
        return dao.findUserNoMobile();
    }


}
